package com.par;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

public class SettingsActivity extends Activity {
	public static final String APP_PREFERENCES = "mysettings";
	public static final String APP_PREFERENCES_AUTO_UPDATE = "AutoUpdate";

	private CheckBox autoUpdateCheckBox;
	
	private boolean isAutoUpdate;
	
	private SharedPreferences settings;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
		
		autoUpdateCheckBox = (CheckBox) findViewById(R.id.updateDatabaseCheckBox);
		autoUpdateCheckBox.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				isAutoUpdate = !isAutoUpdate;
			}
		});
		
		isAutoUpdate = settings.getBoolean(APP_PREFERENCES_AUTO_UPDATE, false);
		autoUpdateCheckBox.setChecked(isAutoUpdate);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}
	
	public void buttonOk_Click(View v ){
		saveSettings(isAutoUpdate);
		finish();
	}
	
	public void buttonCancel_Click(View v ){
		finish();
	}
	
	private void saveSettings(boolean value) {
		Editor editor = settings.edit();
		editor.putBoolean(APP_PREFERENCES_AUTO_UPDATE, value);
		editor.commit();
	}
}
